<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Models\Ads;
use App\Models\AdsPosition;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use App\Models\Attachment;
use Illuminate\Support\Facades\Redirect;

class AdsController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!$this->user->can('viewContentAds')) abort(405);
        
        $type = ['图片','文字', 'Flash'];  //广告类型
        $app_arr = Ads::$app_arr;  //所属app
        $ads = Ads::orderBy('id', 'DESC')->paginate(20);
        /*处理广告位名称*/
        foreach ($ads as $key => $val)
        {
            $ads[$key]['ads_position'] = $val->getPositionName['title'];
            $ads[$key]['type_name'] = $type[$val->type];
            $ads[$key]['app_name'] = '';
            if ($val->app)
                $ads[$key]['app_name'] = $app_arr[$val->app];
        }

        return view('admin.ads.index')->with('ads', $ads)->with('position', []);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!$this->user->can('viewContentAds')) abort(405);
        //
        $postion = AdsPosition::all();
        $ads = Ads::find(0);
        $app_arr = Ads::$app_arr;

        return view('admin.ads.add')->with('position', $postion)->with('ads', $ads)
               ->with('blademodel', 'add')
               ->with('app', $app_arr);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!$this->user->can('editContentAds')) 
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        //
        $post = Input::all();
        if ($post['id'] > 0)
            $ads = Ads::find($post['id']);
        else
            $ads = new Ads;
        /*处理POST来的数据*/
        foreach (array_except($post, ['_token', 'id', 'thumb']) as $key => $val)
        {
            $ads->$key = $val;
        }

        if ($ads->save()) {
            /*处理上传附件*/
            if (isset($post['thumb']) AND $post['thumb'] > 0) {
                $attach = Attachment::find($post['thumb']);
                $attach->app_id = $post['id'] > 0 ? $post['id']: $ads->id;
                $attach->save();
            }

            return redirect("admin/content/ads");
        }
        else
            return Redirect::back()->withInput()->withErrors('保存失败!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!$this->user->can('viewContentAds'))  abort(405);
        //
        $app_arr = Ads::$app_arr;
        $ads = Ads::find($id);
        $postion = AdsPosition::all();
        /*广告的图片*/
        if ($ads->type === 0)
            $attach = Attachment::where(['app' => 'ads', 'app_id' => $id])->first();
        else
            $attach = [];
        return view('admin.ads.edit')->with('ads', $ads)->with('position', $postion)
            ->with('attach', $attach)->with('app', $app_arr);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        if(!$this->user->can('deleteContentAds'))
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        //
        $ads = Ads::find($id);
        if ($ads->delete())
            return  Response::json(array('status'=>'success','msg'=>'广告 [' . str_limit($ads->title, 15, '...') . '] 删除成功'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
